
import Box1 from "./Box1";
import Box2 from "./Box2";
import Box3 from "./Box3";
/**
 * 游戏控制脚本。定义了几个Box，createBoxInterval等变量，能够在IDE显示及设置该变量
 * 更多类型定义，请参考官方文档
 */
export default class GameControl extends Laya.Script {
    /** @prop {name:box1,tips:"掉落容器预制体对象1",type:Prefab}*/
    box1: Laya.Prefab;
    private box_1: Laya.Sprite;
    /** @prop {name:box2,tips:"掉落容器预制体对象2",type:Prefab}*/
    box2: Laya.Prefab;
    private box_2: Laya.Sprite;
    /** @prop {name:box3,tips:"掉落容器预制体对象3",type:Prefab}*/
    box3: Laya.Prefab;
    private box_3: Laya.Sprite;
    /**是否已经开始游戏 */
    private _started: boolean = false;
    /**子弹和盒子所在的容器对象 */
    private _gameBox: Laya.Sprite;

    /**盒子等级 */
	private _level: number;

    /* 构造函数 */
    constructor() { super(); }

    onEnable(): void {
        this._gameBox = this.owner.getChildByName("gameBox") as Laya.Sprite;
        
    }

    onUpdate(): void {
    }

    createBox(): void {
        /* 盒子等级：1/2/3 随机掉落 */
        this._level = Math.round(Math.random() * 2) + 1;

         //使用对象池创建盒子
        switch(this._level){
            case 1 :
                this.box_1 = Laya.Pool.getItemByCreateFun("box1", this.box1.create, this.box1);
                this.box_1.pos(Laya.stage.width/2, 0);
                this._gameBox.addChild(this.box_1);
                break;
            case 2 :
                this.box_2 = Laya.Pool.getItemByCreateFun("box2", this.box2.create, this.box2);
                this.box_2.pos(Laya.stage.width/2, 0);
                this._gameBox.addChild(this.box_2);
                break;
            case 3 :
                this.box_3 = Laya.Pool.getItemByCreateFun("box3", this.box3.create, this.box3);
                this.box_3.pos(Laya.stage.width/2, 0);
                this._gameBox.addChild(this.box_3);
                break;
        }
    }

    /* 鼠标按下 */
    onClick(e: Laya.Event): void {
        this.createBox();
        
    }

    /* 鼠标移动 */
    onStageMouseMove(e: Laya.Event): void {
        let mouseX: number = e.stageX;
        console.log(mouseX);

        let hasBox1:boolean = this._gameBox.getChildByName("box1") == null ? false : true;

        let hasBox2:boolean = this._gameBox.getChildByName("box2") == null ? false : true;

        let hasBox3:boolean = this._gameBox.getChildByName("box3") == null ? false : true;

        if(!hasBox1 || !hasBox2 || !hasBox3){
            return;
        }

        if(this.box1){
            this.box_1.x = mouseX; 
        }
        if(this.box2){
            this.box_2.x = mouseX; 
        }
        if(this.box3){
            this.box_3.x = mouseX; 
        }
    }

    // /* 鼠标抬起 */
    // onStageMouseUp(e: Laya.Event): void {

    //     if(this.box1){
    //         var _rig:Laya.RigidBody;
    //         _rig = this.box_1.getComponent(Laya.RigidBody);
    //         _rig.type = "dynamic";
    //     }

    //     if(this.box2){
    //         var _rig:Laya.RigidBody;
    //         _rig = this.box_2.getComponent(Laya.RigidBody);
    //         _rig.type = "dynamic";
    //     }

    //     if(this.box3){
    //         var _rig:Laya.RigidBody;
    //         _rig = this.box_3.getComponent(Laya.RigidBody);
    //         _rig.type = "dynamic";
    //     }
    // }

    onStageClick(e: Laya.Event): void {
        //停止事件冒泡，提高性能，当然也可以不要
        e.stopPropagation();
    }

    /**开始游戏，通过激活本脚本方式开始游戏*/
    startGame(): void {
        if (!this._started) {
            this._started = true;
            this.enabled = true;
        }
    }

    /**结束游戏，通过非激活本脚本停止游戏 */
    stopGame(): void {
        this._started = false;
        this.enabled = false;
        this._gameBox.removeChildren();
    }
}